package com.qy.service.impl;

import com.qy.dao.EvaluationMapper;
import com.qy.model.Charge;
import com.qy.model.Evaluation;
import com.qy.model.Policy;
import com.qy.model.Unit;
import com.qy.service.ChargeService;
import com.qy.service.EvaluationService;
import com.qy.base.core.AbstractService;
import com.qy.service.PolicyService;
import com.qy.service.UnitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * Created by zaq on 2018/07/10.
 */
@Service
@Transactional
public class EvaluationServiceImpl extends AbstractService<Evaluation> implements EvaluationService {
    @Resource
    private EvaluationMapper qyEvaluationMapper;

    @Resource
    private PolicyService policyService;

    @Resource
    private UnitService unitService;

    @Resource
    private ChargeService chargeService;

    @Override
    public List<Evaluation> selectEvaluationByUserId(Policy policy) {
        return qyEvaluationMapper.selectEvaluationByUserId(policy);
    }

    @Override
    public List<Map> selectEvaluationListByUserId(Evaluation evaluation) {
        return qyEvaluationMapper.selectEvaluationListByUserId(evaluation);
    }

    @Override
    public List<Map> evaluationList(String user_name, String phone, Integer policy_type, String policy_need) {
        return qyEvaluationMapper.evaluationList(user_name, phone, policy_type, policy_need);
    }

    @Override
    public Map<String, Object> selectUnitListAndChargeList(List<Map> list) {
        Set<Policy> list1 = new HashSet<>();
        Set<Unit> list2 = new HashSet<>();
        Set<Charge> list3 = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            Policy policy = policyService.findById(Integer.valueOf(list.get(i).get("id").toString()));
            list1.add(policy);
            if (policy.getUnit_ids() != null && !policy.getUnit_ids().isEmpty()) {
                if (policy.getUnit_ids().contains(",")) {
                    String[] ids = policy.getUnit_ids().split(",");
                    for (int j = 0; j < ids.length; j++) {
                        Unit unit = unitService.findById(Integer.valueOf(ids[i]));
                        list2.add(unit);
                    }
                } else {
                    Unit unit = unitService.findById(Integer.valueOf(policy.getUnit_ids()));
                    list2.add(unit);
                }
            }
            if (policy.getCharge_ids() != null && !policy.getCharge_ids().isEmpty()) {
                if (policy.getCharge_ids().contains(",")) {
                    String[] ids = policy.getCharge_ids().split(",");
                    for (int k = 0; k < ids.length; k++) {
                        Charge charge = chargeService.findById(Integer.valueOf(ids[i]));
                        list3.add(charge);
                    }
                } else {
                    Charge charge = chargeService.findById(Integer.valueOf(policy.getCharge_ids()));
                    list3.add(charge);
                }
            }
        }

        Map<String, Object> map = new HashMap<>();
        if (list1.size() == 0) {
            map.put("policyList", null);
        } else {
            map.put("policyList", list1);
        }
        if (list2.size() == 0) {
            map.put("unitList", null);
        } else {
            map.put("unitList", list2);
        }
        if (list3.size() == 0) {
            map.put("chargeList", null);
        } else {
            map.put("chargeList", list3);
        }
        return map;
    }
}
